/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Configuration; 
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;
using Shuriken.Core.Utils; 

namespace Shuriken.Core
{
    /// <summary>
    /// Class storing global application related information. 
    /// </summary>
    public class ApplicationEnvironment
    {
        private static string m_ProductName;
        private static string m_InstallationDirectory;
        private static string m_ProductNameShort;
        private static string m_ProductVersion;
        private static string m_ProductCopyright;
        private static string m_ProductBuildNumber;
        private static string m_FileVersion;
        private static string m_mainExecutableName;
        private static string m_ApplicationDataDirectory;
        private static string m_ApplicationPluginDirectory; 
        private static string m_LocalApplicationDataDirectory;
        private static string m_LogFilePath;
        private static CommandLineParameters m_Parameters; 
        
        private static ApplicationDiagnostics m_ApplicationDiagnostics;
        private static CorePreferences m_Preferences;
        private static ApplicationSettingsBase m_GlobalSettings; 

        static ApplicationEnvironment()
        {
            m_ProductNameShort = string.Empty; 
        }

        public static void Initialize(ApplicationSettingsBase globalSettings)
        {
            Assembly assembly = Assembly.GetCallingAssembly();

            m_GlobalSettings = globalSettings; 
            m_InstallationDirectory = Path.GetDirectoryName(assembly.Location);
            m_mainExecutableName = Path.GetFileName(assembly.Location);

            FileVersionInfo info = FileVersionInfo.GetVersionInfo(assembly.Location);
            m_ProductName = info.ProductName;
            m_ProductVersion = string.Format("{0}.{1}", info.ProductMajorPart, info.ProductMinorPart);
            m_ProductBuildNumber = info.ProductBuildPart.ToString();
            m_FileVersion = info.FileVersion.ToString();
            m_ProductCopyright = GetAssemblyCopyright();

            m_ApplicationDataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ApplicationEnvironment.ProductName);
            if (!Directory.Exists(m_ApplicationDataDirectory))
            {
                Directory.CreateDirectory(m_ApplicationDataDirectory);
            }

            m_LocalApplicationDataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ApplicationEnvironment.ProductName);
            if (!Directory.Exists(m_LocalApplicationDataDirectory))
            {
                Directory.CreateDirectory(m_LocalApplicationDataDirectory);
            }

            m_ApplicationPluginDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins");
            if (!Directory.Exists(m_ApplicationPluginDirectory))
            {
                Directory.CreateDirectory(m_ApplicationPluginDirectory);
            }

            
            InitializeLogFilePath();
            m_ApplicationDiagnostics = new ApplicationDiagnostics(m_LogFilePath);            
        }

        private static string GetAssemblyCopyright()
        {
            // Get all Copyright attributes on this assembly
            object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
            // If there aren't any Copyright attributes, return an empty string
            if (attributes.Length == 0)
                return "";
            // If there is a Copyright attribute, return its value
            return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
        }

        /// <summary>
        /// Initalizes log file path
        /// </summary>
        private static void InitializeLogFilePath()
        {
            m_LogFilePath = ApplicationEnvironment.m_LocalApplicationDataDirectory;
            m_LogFilePath = Path.Combine(m_LogFilePath, string.Format("{0}.log", m_ProductName));
        }

        /// <summary>
        /// Product name
        /// </summary>
        public static string ProductName
        {
            get { return m_ProductName; }
        }

        /// <summary>
        /// Short Product name
        /// </summary>
        /// <remarks>
        /// Make sure that the short product name is always one word, as this will be used for folder name and registyr key name. 
        /// </remarks>
        public static string ProductNameShort
        {
            get { return m_ProductNameShort; }
            set { m_ProductNameShort = value; }
        }


        /// <summary>
        /// Product version 
        /// </summary>
        public static string ProductVersion
        {
            get { return m_ProductVersion; }
        }

        /// <summary>
        /// Product BuildNumber
        /// </summary>
        public static string ProductBuildNumber
        {
            get { return m_ProductBuildNumber; }
        }

        public static string ProductCopyright
        {
            get { return m_ProductCopyright;  }
        }
        /// <summary>
        /// File Version 
        /// </summary>
        public static string FileVersion
        {
            get { return m_FileVersion; }
        }

        /// <summary>
        /// Application Data Directory 
        /// </summary>
        public static string ApplicationDataDirectory
        {
            get { return m_ApplicationDataDirectory; }
        }

        /// <summary>
        /// Local Application Data Directory 
        /// </summary>
        public static string LocalApplicationDataDirectory
        {
            get { return m_LocalApplicationDataDirectory; }
        }

        /// <summary>
        /// Application's Plugin Directory 
        /// </summary>
        public static string ApplicationPluginDirectory
        {
            get { return m_ApplicationPluginDirectory; }
        }

        /// <summary>
        /// Returns the full path to the executable, 
        /// </summary>
        public static string ExecutablePath
        {
            get
            {
                return Application.ExecutablePath; 
            }
        }

        /// <summary>
        /// Returns the full path to the log file. 
        /// </summary>
        public static string LogFilePath
        {
            get
            {
                return m_LogFilePath; 
            }
        }

        /// <summary>
        /// Diagnostics functionality
        /// </summary>
        public static ApplicationDiagnostics Diagnostics 
        {
            get 
            {
                return m_ApplicationDiagnostics; 
            }
        }

        /// <summary>
        /// Application Preferences 
        /// </summary>
        public static CorePreferences Preferences
        {
            get
            {
                if(m_Preferences == null)
                {
                    m_Preferences = new CorePreferences();
                }

                return m_Preferences;
            }
        }

        public static CommandLineParameters Parameters
        {
            get
            {
                return m_Parameters; 
            }
            set
            {
                m_Parameters = value;
            }
        }
    }
}
